package com.example.sringboot_web;

import com.example.sringboot_web.filter.myfilter1;
import com.example.sringboot_web.filter.myfilter2;
import com.example.sringboot_web.listener.Mylistener1;
import com.example.sringboot_web.servlet.myServlet1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.Filter;


@EnableAspectJAutoProxy
@SpringBootApplication
//@ServletComponentScan
public class SringbootWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SringbootWebApplication.class, args);
    }

    //自定义容器注册进入servlet容器
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new myServlet1());
        bean.addUrlMappings("/myservlet1");
        return bean;
    }

    @Bean
    public Filter myfilter1() {
        return new myfilter1();
    }


    @Bean
    public Filter myfilter2() {
        return new myfilter2();
    }


    //拦截器
    @Bean
    public FilterRegistrationBean filterRegistrationBean1() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        //filterRegistrationBean.addUrlPatterns("/myservlet1","/myservlet");
        filterRegistrationBean.setFilter(myfilter1());
        filterRegistrationBean.addUrlPatterns("/myservlet","/index");
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

    //拦截器
    @Bean
    public FilterRegistrationBean filterRegistrationBean2() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        //filterRegistrationBean.addUrlPatterns("/myservlet1","/myservlet");
        filterRegistrationBean.setFilter(myfilter2());
        filterRegistrationBean.addUrlPatterns("/myservlet");
        filterRegistrationBean.setOrder(2);
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean() {
        ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new Mylistener1());
        return bean;
    }
}
